package com.yc.service.impl;

import com.yc.mapper.WxScheduleMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 小程序课程表服务实现类
 * @author zxy
 * @date : 2023-6-28
 */
@Api("微信管理课程表实现类")
@Service
public class WxScheduleServiceImp {
    @Autowired
    private WxScheduleMapper wxScheduleMapper;

    /**
     * 查询所有学年（用于页面学年选择器选项
     * @author zxy
     * @return 学年
     */
    @ApiOperation("查询所有学年")
    public List<Map<String, Object>> selectschoolyear(){
        return wxScheduleMapper.selectschoolyear();
    }

    /**
     * 查询当天是哪个学期的哪一周
     * @author zxy
     * @param day 当天日期 格式：2023-09-04
     * @return 返回周和学期
     */
    @ApiOperation("查询当天是哪个学期的哪一周")
    public Map<String, Object> selectwhichweekandyear(String day){
        return wxScheduleMapper.selectwhichweekandyear(day);
    }

    /**
     * 通过传入当天日期来查询 所在的学期 和 所在的周次 和 当周的全部日期
     * @author zxy
     * @param day 当天的日期
     * @return 所在的学期 和 所在的周次 和 当周的全部日期
     */
    @ApiOperation("通过传入当天日期来查询 所在的学期 和 所在的周次 和 当周的全部日期")
    public Map<String,Object> selectdayyearweek(String day){
        Map<String,Object> yearandweek = wxScheduleMapper.selectwhichweekandyear(day);
        String week = (String) yearandweek.get("week");
        String year = (String) yearandweek.get("school_year");
        List<String> allday1 = wxScheduleMapper.selectalldayinthisweek(week,year);
        List<String> allday = new ArrayList<>();
        for (String s : allday1) {
            String ss = s.substring(5).replace('-','/');
            allday.add(ss);
        }
        Map<String,Object> dayweekyear = new HashMap<>();
        dayweekyear.put("week",week);
        dayweekyear.put("year",year);
        dayweekyear.put("allday",allday);
        return dayweekyear;
    }

    /**
     * 通过周次学年查询该学年该周次下的日期
     * @author zxy
     * @param week 周次
     * @param year 学年（学期
     * @return
     */
    @ApiOperation("通过周次学年查询该学年该周次下的日期")
    public List<String> selectdaybyweek(String week, String year){
        List<String> allday1 = wxScheduleMapper.selectalldayinthisweek(week,year);
        List<String> allday = new ArrayList<>();
        for (String s : allday1) {
            String ss = s.substring(5).replace('-','/');
            allday.add(ss);
        }
        return allday;
    }

    /**
     *  通过学生id和周次查询该周全部课程
     * @author zxy
     * @param stuid 学生id
     * @param week 周次
     * @return 课程id 课程名 第几节 老师名 教室 上课时间 显示的颜色 高度偏移量 显示长度 左边偏移量
     * @throws ParseException
     */
    @ApiOperation("通过学生id和周次查询该周全部课程")
    public List<Map<String,Object>> selectstuclass(String stuid,String week) throws ParseException {
        List<Map<String,Object>> allclass = wxScheduleMapper.selectstuclass(week,stuid);
        String[] weekDays = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

        Calendar cal = Calendar.getInstance();
        // 将查询的日期转化成星期几
        for (Map<String, Object> map : allclass) {
            String xq = (String) map.get("date");
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(xq.substring(0,10));
            cal.setTime(date);
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1 < 0 ? 0 : cal.get(Calendar.DAY_OF_WEEK) - 1;
            xq = weekDays[w];
            map.put("xq",xq);
        }

        // 设置课程的偏移量
        for (Map<String, Object> objectMap : allclass) {
            String xq = (String) objectMap.get("xq");
            // 设置该课程向右的偏移量(Left)
            if (xq.equals("星期一")){
                objectMap.put("left","150rpx");
            }else if (xq.equals("星期二")){
                objectMap.put("left","234rpx");
            }else if (xq.equals("星期三")){
                objectMap.put("left","318rpx");
            }else if (xq.equals("星期四")){
                objectMap.put("left","402rpx");
            }else if (xq.equals("星期五")){
                objectMap.put("left","486rpx");
            }else if (xq.equals("星期六")){
                objectMap.put("left","570rpx");
            }else if (xq.equals("星期天")){
                objectMap.put("left","654rpx");
            }
            String js = (String) objectMap.get("classtime");// 获取节数
            // 设置课程的高度（长度 默认一小节高为110rpx 一大节为230rpx
            objectMap.put("height","110rpx");
            String show = (String) objectMap.get("coursename");
            objectMap.put("show",show);// 用于显示的课程名
            if (js.length()>1){ // 最多是两小节 如果长度>1 这说明是一大节 因此将该课程的高度改为两节的高度
                objectMap.put("height","230rpx");
            }else{
                show = (String) objectMap.get("coursename");
                objectMap.put("show",show.substring(0,4));
            }
            // 设置课程偏移的高度（Top)
            String start = js.substring(0,1);// 截取开始的节数
            if (start.equals("1")){
                objectMap.put("top","335rpx");
            } else if(start.equals("2")){
                objectMap.put("top","455rpx");
            } else if(start.equals("3")){
                objectMap.put("top","575rpx");
            } else if(start.equals("4")){
                objectMap.put("top","695rpx");
            } else if(start.equals("5")){
                objectMap.put("top","832rpx");
            } else if(start.equals("6")){
                objectMap.put("top","952rpx");
            } else if(start.equals("7")){
                objectMap.put("top","1072rpx");
            } else if(start.equals("8")){
                objectMap.put("top","1192rpx");
            } else if(start.equals("9")){
                objectMap.put("top","1324rpx");
            } else if(start.equals("10")){
                objectMap.put("top","1444rpx");
            }
            if(Objects.nonNull(objectMap.get("classweek")) && objectMap.get("classweek")!=null){
                String[] classweeksArray = String.valueOf(objectMap.get("classweek")).split(",");
                List<Integer> integerList = new ArrayList<>();
                for (String classweek : classweeksArray) {
                    integerList.add(Integer.valueOf(classweek));
                }
                objectMap.put("classweek",generateRanges(integerList));
            }
        }
        return allclass;
    }

    /**
     * 计算上课周次
     * @param list
     * @return
     */
    public static String generateRanges(List<Integer> list) {
        if (list == null || list.isEmpty()) {
            return "";
        }

        Collections.sort(list);
        List<String> ranges = new ArrayList<>();

        int start = list.get(0);
        int end = list.get(0);

        for (int i = 1; i < list.size(); i++) {
            if (list.get(i) == end + 1) {
                end = list.get(i);
            } else {
                if (start == end) {
                    ranges.add(String.valueOf(start));
                } else {
                    ranges.add(start + "-" + end);
                }
                start = list.get(i);
                end = list.get(i);
            }
        }

        if (start == end) {
            ranges.add(String.valueOf(start));
        } else {
            ranges.add(start + "-" + end);
        }

        return String.join(",", ranges);
    }

    /**
     *  通过老师id和周次查询该周全部课程
     * @author zxy
     * @param tid 学生id
     * @param week 周次
     * @return 课程id 课程名 第几节 班级名 教室 上课时间 显示的颜色 高度偏移量 显示长度 左边偏移量
     * @throws ParseException
     */
    @ApiOperation("通过老师id和周次查询该周全部课程")
    public List<Map<String,Object>> selectteacherclass(String tid,String week) throws ParseException {
        List<Map<String,Object>> allclass = wxScheduleMapper.selectteaclass(week,tid);
        String[] weekDays = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        // 将查询的日期转化成星期几
        for (Map<String, Object> map : allclass) {
            String xq = (String) map.get("date");
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(xq.substring(0,10));
            cal.setTime(date);
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1 < 0 ? 0 : cal.get(Calendar.DAY_OF_WEEK) - 1;
            xq = weekDays[w];
            map.put("xq",xq);
        }

        // 设置课程的偏移量
        for (Map<String, Object> objectMap : allclass) {
            String xq = (String) objectMap.get("xq");
            // 设置该课程向右的偏移量(Left)
            if (xq.equals("星期一")){
                objectMap.put("left","150rpx");
            }else if (xq.equals("星期二")){
                objectMap.put("left","234rpx");
            }else if (xq.equals("星期三")){
                objectMap.put("left","318rpx");
            }else if (xq.equals("星期四")){
                objectMap.put("left","402rpx");
            }else if (xq.equals("星期五")){
                objectMap.put("left","486rpx");
            }else if (xq.equals("星期六")){
                objectMap.put("left","570rpx");
            }else if (xq.equals("星期天")){
                objectMap.put("left","654rpx");
            }
            String js = (String) objectMap.get("classtime");// 获取节数
            // 设置课程的高度（长度 默认一小节高为110rpx 一大节为230rpx
            objectMap.put("height","110rpx");
            String show = (String) objectMap.get("coursename");
            objectMap.put("show",show);
            if (js.length()>1){ // 最多是两小节 如果长度>1 这说明是一大节 因此将该课程的高度改为两节的高度
                objectMap.put("height","230rpx");
            }else{
                show = (String) objectMap.get("coursename");
                objectMap.put("show",show.substring(0,4));
            }
            // 设置课程偏移的高度（Top)
            String start = js.substring(0,1);// 截取开始的节数
            if (start.equals("1")){
                objectMap.put("top","335rpx");
            } else if(start.equals("2")){
                objectMap.put("top","455rpx");
            } else if(start.equals("3")){
                objectMap.put("top","575rpx");
            } else if(start.equals("4")){
                objectMap.put("top","695rpx");
            } else if(start.equals("5")){
                objectMap.put("top","832rpx");
            } else if(start.equals("6")){
                objectMap.put("top","952rpx");
            } else if(start.equals("7")){
                objectMap.put("top","1072rpx");
            } else if(start.equals("8")){
                objectMap.put("top","1192rpx");
            } else if(start.equals("9")){
                objectMap.put("top","1324rpx");
            } else if(start.equals("10")){
                objectMap.put("top","1444rpx");
            }
            if(Objects.nonNull(objectMap.get("classweek")) && objectMap.get("classweek")!=null){
                String[] classweeksArray = String.valueOf(objectMap.get("classweek")).split(",");
                List<Integer> integerList = new ArrayList<>();
                for (String classweek : classweeksArray) {
                    integerList.add(Integer.valueOf(classweek));
                }
                objectMap.put("classweek",generateRanges(integerList));
            }

        }
        return allclass;
    }
}
